package com.remark.controoler;

import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.remark.service.ESManagerService;
import com.remark.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private SearchService searchService;

    @Autowired
    private ESManagerService esManagerService;

    @GetMapping("/mapping")
    public Result mapingES(){
        esManagerService.createIndexAndMapping();
        return new Result(true, StatusCode.OK,"映射成功");
    }

    @GetMapping
    public Result search(@RequestParam Map<String,String> searchMap) throws Exception {
        Map search = searchService.search(searchMap);
        return new Result(true, StatusCode.OK,"搜索成功",search);
    }

    @PostMapping("/addAll")
    public Result addAll(){
        esManagerService.importAll();
        return new Result(true, StatusCode.OK,"添加搜索成功");
    }

    @PostMapping("/add/{id}")
    public Result addById(@PathVariable("id")Integer id){
        esManagerService.importData2ESById(id);
        return new Result(true, StatusCode.OK,"添加搜索成功");
    }


}
